package com.project.hqshorttaxi.service.impl;

import com.project.hqshorttaxi.dto.OrderParam;
import com.project.hqshorttaxi.entity.TaxiOrder;
import com.project.hqshorttaxi.mapper.TaxiOrderMapper;
import com.project.hqshorttaxi.service.OrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private TaxiOrderMapper orderMapper;

    @Override
    public TaxiOrder getById(Integer id) {
        return orderMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<TaxiOrder> list(OrderParam param) {
        Example example = new Example(TaxiOrder.class);
        Example.Criteria c = example.createCriteria();
        if (!StringUtils.isEmpty(param.getPlateNumber())) {
            c.andLike("plateNumber", "%" + param.getPlateNumber() + "%");
        }
        if (param.getBeginTime() != null) {
            c.andGreaterThanOrEqualTo("beginTime", param.getBeginTime());
        }
        if (param.getEndTime() != null) {
            c.andLessThanOrEqualTo("endTime", param.getEndTime());
        }
        example.orderBy("recieveTime").desc();
        List<TaxiOrder> list = orderMapper.selectByExample(example);
        return list;
    }

    @Override
    public TaxiOrder getByPlateNumber(String plateNumber) {
        Example example = new Example(TaxiOrder.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("plateNumber", plateNumber);
        example.orderBy("recieveTime").desc();
        List<TaxiOrder> list = orderMapper.selectByExample(example);
        return list.size() == 0 ? null : list.get(0);
    }
}
